package com.zbkj.front.controller.merchant;

import com.zbkj.common.model.traceability.TraceabilityLogs;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.traceability.TraceabilityLogsPageRequest;
import com.zbkj.common.request.traceability.TraceabilityLogsRequest;
import com.zbkj.common.result.CommonResult;
import com.zbkj.service.service.TraceabilityLogsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 追溯日志管理控制器
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: xiaolic
 * +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("api/merchant/traceability-logs")
@Api(tags = "商品溯源-追溯日志管理")
public class TraceabilityLogsController {

    @Autowired
    private TraceabilityLogsService traceabilityLogsService;

    /**
     * 分页查询追溯日志列表
     * @param request 查询参数
     * @return 日志列表
     */
    @ApiOperation(value = "分页查询追溯日志列表")
    @GetMapping("/list")
    public CommonResult<CommonPage<TraceabilityLogs>> getList(@Validated TraceabilityLogsPageRequest request) {
        return CommonResult.success(CommonPage.restPage(traceabilityLogsService.getPageList(request)));
    }

    /**
     * 添加追溯日志
     * @param request 日志信息
     * @return 添加结果
     */
    @ApiOperation(value = "添加追溯日志")
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody @Validated TraceabilityLogsRequest request) {
        if (traceabilityLogsService.create(request)) {
            return CommonResult.success("添加成功");
        } else {
            return CommonResult.failed("添加失败");
        }
    }

    /**
     * 修改追溯日志
     * @param request 日志信息
     * @return 修改结果
     */
    @ApiOperation(value = "修改追溯日志")
    @PostMapping("/update")
    public CommonResult<String> update(@RequestBody @Validated TraceabilityLogsRequest request) {
        if (request.getId() == null) {
            return CommonResult.failed("日志ID不能为空");
        }
        if (traceabilityLogsService.update(request)) {
            return CommonResult.success("修改成功");
        } else {
            return CommonResult.failed("修改失败");
        }
    }

    /**
     * 删除追溯日志
     * @param id 日志ID
     * @return 删除结果
     */
    @ApiOperation(value = "删除追溯日志")
    @ApiImplicitParam(name = "id", value = "日志ID", required = true, dataType = "Long", paramType = "path")
    @PostMapping("/delete/{id}")
    public CommonResult<String> delete(@PathVariable Long id) {
        if (traceabilityLogsService.delete(id)) {
            return CommonResult.success("删除成功");
        } else {
            return CommonResult.failed("删除失败");
        }
    }

    /**
     * 获取追溯日志详情
     * @param id 日志ID
     * @return 日志详情
     */
    @ApiOperation(value = "获取追溯日志详情")
    @ApiImplicitParam(name = "id", value = "日志ID", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/detail/{id}")
    public CommonResult<TraceabilityLogs> getDetail(@PathVariable Long id) {
        TraceabilityLogs detail = traceabilityLogsService.getDetail(id);
        if (detail != null) {
            return CommonResult.success(detail);
        } else {
            return CommonResult.failed("日志不存在");
        }
    }

    /**
     * 根据批次ID获取所有日志列表
     * @param batchId 批次ID
     * @return 日志列表
     */
    @ApiOperation(value = "根据批次ID获取所有日志列表")
    @ApiImplicitParam(name = "batchId", value = "批次ID", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/list/by-batch/{batchId}")
    public CommonResult<List<TraceabilityLogs>> getLogsByBatchId(
            @PathVariable Long batchId) {
        return CommonResult.success(traceabilityLogsService.getLogsByBatchId(batchId));
    }

    /**
     * 根据可追溯项ID和批次ID获取所有日志列表
     * @param traceableItemId 可追溯项ID
     * @param batchId 批次ID
     * @return 日志列表
     */
    @ApiOperation(value = "查询追溯日志列表", notes = "根据可追溯项ID和批次ID查询日志列表。查询策略：优先查询可追溯项的专属日志，如果没有找到专属日志，则返回所属批次的共享日志")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "traceableItemId", value = "可追溯项ID", required = true, dataType = "Long", paramType = "path"),
        @ApiImplicitParam(name = "batchId", value = "批次ID", required = true, dataType = "Long", paramType = "path")
    })
    @GetMapping("/list/{batchId}/{traceableItemId}")
    public CommonResult<List<TraceabilityLogs>> getLogsByTraceableItemIdOrBatchId(
            @PathVariable Long traceableItemId,
            @PathVariable Long batchId) {
        return CommonResult.success(traceabilityLogsService.getLogsByTraceableItemIdOrBatchId(traceableItemId, batchId));
    }
}
